<template>
  <div class="chat-display">
    <div v-if="messages.length === 0" class="empty-chat">
      <p>开始新的对话吧！</p>
    </div>
    <div v-else class="messages-container">
      <div
        v-for="(message, index) in messages"
        :key="message.id"
        :class="['message-wrapper', { 'user-message': message.role === 'user', 'ai-message': message.role === 'assistant' }]"
      >
        <div class="message-header">
          <span class="message-role">{{ message.role === 'user' ? '你' : 'AI' }}</span>
          <span class="message-time">{{ formatTime(message.timestamp) }}</span>
        </div>
        <div class="message-content">
          <div v-html="renderMarkdown(message.message)"></div>
          <!-- 显示图片附件 -->
          <div v-if="message.files && message.files.length > 0" class="message-attachments">
            <div v-for="(file, fileIndex) in message.files" :key="fileIndex">
              <img 
                v-if="file.type && file.type.startsWith('image/')" 
                :src="file.url" 
                :alt="file.name || `图片 ${fileIndex + 1}`"
                class="message-image"
                @click="openImagePreview(file.url)"
              />
            </div>
          </div>
        </div>
        <!-- 只为AI回复添加创建分支按钮 -->
        <div v-if="message.role === 'assistant'" class="message-actions">
          <button class="branch-button" @click="handleCreateBranch(message.id)">
            创建分支
          </button>
        </div>
      </div>
      <!-- 图片预览模态框 -->
      <div v-if="previewImageUrl" class="image-preview-modal" @click="closeImagePreview">
        <div class="preview-content" @click.stop>
          <img :src="previewImageUrl" alt="预览图片" class="preview-large" />
          <button class="close-preview" @click="closeImagePreview">×</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { marked } from 'marked';
import { ref } from 'vue';

export default {
  name: 'ChatDisplay',
  props: {
    messages: {
      type: Array,
      default: () => []
    }
  },
  setup(props, { emit }) {
    const previewImageUrl = ref('');

    const formatTime = (timestamp) => {
      const date = new Date(timestamp);
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      return `${hours}:${minutes}`;
    };

    const handleCreateBranch = (messageId) => {
      emit('create-branch', messageId);
    };

    const renderMarkdown = (text) => {
      // 设置 marked 选项
      marked.setOptions({
        breaks: true, // 支持回车换行
        gfm: true,   // 支持 GitHub 风格的 Markdown
        headerIds: false, // 不生成 header id
        mangle: false, // 不混淆邮箱
        silent: true // 忽略错误
      });
      return marked.parse(text);
    };

    const openImagePreview = (url) => {
      previewImageUrl.value = url;
    };

    const closeImagePreview = () => {
      previewImageUrl.value = '';
    };

    return {
      formatTime,
      handleCreateBranch,
      renderMarkdown,
      previewImageUrl,
      openImagePreview,
      closeImagePreview
    };
  }
};
</script>

<style scoped>
.chat-display {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  overflow-y: auto;
}

.empty-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: #999;
  font-size: 16px;
}

.messages-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.message-wrapper {
  display: flex;
  flex-direction: column;
  max-width: 70%;
}

.user-message {
  align-self: flex-end;
}

.ai-message {
  align-self: flex-start;
}

.message-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 5px;
}

.message-role {
  font-weight: bold;
  font-size: 14px;
}

.user-message .message-role {
  color: #0066cc;
}

.ai-message .message-role {
  color: #666666;
}

.message-time {
  font-size: 12px;
  color: #999;
}

.message-content {
  padding: 10px 15px;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  word-wrap: break-word;
}

.user-message .message-content {
  background-color: #e3f2fd;
}

.message-content p {
  margin: 0 0 8px 0;
  line-height: 1.6;
  font-size: 14px;
  color: #333;
}

.message-content h1, .message-content h2, .message-content h3 {
  margin: 16px 0 8px 0;
  font-weight: 600;
}

.message-content h1 {
  font-size: 24px;
  border-bottom: 1px solid #eee;
  padding-bottom: 4px;
}

.message-content h2 {
  font-size: 20px;
}

.message-content h3 {
  font-size: 16px;
}

.message-content ul, .message-content ol {
  margin: 8px 0 8px 20px;
  padding-left: 0;
}

.message-content li {
  margin-bottom: 4px;
}

.message-content a {
  color: #0366d6;
  text-decoration: none;
}

.message-content a:hover {
  text-decoration: underline;
}

.message-content code {
  background-color: #f6f8fa;
  border-radius: 3px;
  padding: 2px 4px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 12px;
}

.message-content pre {
  background-color: #f6f8fa;
  border-radius: 6px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
}

.message-content pre code {
  background-color: transparent;
  padding: 0;
}

.message-content blockquote {
  border-left: 4px solid #ddd;
  padding-left: 12px;
  color: #666;
  margin: 8px 0;
}

.message-actions {
  margin-top: 5px;
  align-self: flex-end;
}

.branch-button {
  background-color: #f0f0f0;
  color: #333;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.branch-button:hover {
  background-color: #e0e0e0;
}

.branch-button:hover {
  background-color: #e0e0e0;
}
    /* 附件样式 */
    .message-attachments {
      margin-top: 10px;
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .message-image {
      max-width: 100%;
      max-height: 200px;
      border-radius: 4px;
      cursor: pointer;
      transition: transform 0.2s;
      border: 1px solid #e0e0e0;
    }

    .message-image:hover {
      transform: scale(1.02);
    }

    /* 图片预览模态框 */
    .image-preview-modal {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.8);
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 1000;
    }

    .preview-content {
      position: relative;
      max-width: 90%;
      max-height: 90%;
    }

    .preview-large {
      max-width: 100%;
      max-height: 100vh;
      border-radius: 8px;
    }

    .close-preview {
      position: absolute;
      top: -40px;
      right: -40px;
      width: 40px;
      height: 40px;
      background-color: rgba(0, 0, 0, 0.5);
      color: white;
      border: none;
      border-radius: 50%;
      font-size: 24px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: background-color 0.2s;
    }

    .close-preview:hover {
      background-color: rgba(0, 0, 0, 0.8);
    }
  </style>